#include "algorithminputdialog.h"
#include "ui_algorithminputdialog.h"

AlgorithmInputDialog::AlgorithmInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::algorithmInputDialog)
{
    ui->setupUi(this);

    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(clearAndClose()));
    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(okPressed()));
    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(clearAndClose()));

    connect(ui->dllPathButton, SIGNAL(clicked()), this, SLOT(browseDLL()));
    connect(ui->helpPathButton, SIGNAL(clicked()),this, SLOT(browseHelp()));
}

AlgorithmInputDialog::~AlgorithmInputDialog()
{
    delete ui;
}

void AlgorithmInputDialog::prepare()
{
    ui->optimizationComboBox->clear();
    ui->optimizationComboBox->clearEditText();
    ui->optimizationComboBox->addItems(OptimizationStrings);
}

void AlgorithmInputDialog::okPressed()
{
    Algorithm algorithm;
    algorithm.name = ui->nameEdit->text();
    algorithm.optimization = ui->optimizationComboBox->currentText();
    algorithm.dllPath = ui->dllPathEdit->text();
    algorithm.helpPath = ui->helpPathEdit->text();

    emit itemMade(algorithm);
}

void AlgorithmInputDialog::clearAndClose()
{
    ui->nameEdit->clear();
    ui->dllPathEdit->clear();
    ui->helpPathEdit->clear();

    this->close();
}

void AlgorithmInputDialog::browseDLL()
{
    QString path = QFileDialog::getOpenFileName(this, tr("Open DLL file"), "", tr("DLL (*.dll)"));
    ui->dllPathEdit->setText(path);
}

void AlgorithmInputDialog::browseHelp()
{
    QString path = QFileDialog::getOpenFileName(this, tr("Open HTML help file"), "", tr("HTML (*.html)"));
    ui->helpPathEdit->setText(path);
}
